package org.yjc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JournalUtils;
import org.yjc.custom.UserUtils;
import org.yjc.custom.VerifyCodeUtils;
import org.yjc.entity.Book;
import org.yjc.entity.SysRole;
import org.yjc.entity.SysUser;
import org.yjc.repository.SysRoleRepository;
import org.yjc.repository.SysUserRepository;
import org.yjc.service.BookService;
import org.yjc.service.SysUserService;

@Controller
public class MainController {
	@Autowired
	private UserUtils userutils;
	@Autowired
	private SysUserRepository sysuserDAO;
	@Autowired
	private SysUserService sysuserservice;
	@Autowired
	private JournalUtils journalutils;
	@Autowired
	private BookService bookservice;
	@Autowired
	private SysRoleRepository roleDAO;
	//发送者的邮箱账号
	@Value("${spring.mail.username}")
	private String mailusername;
	@Autowired
	JavaMailSender jms;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping(value="/")
	public String main() {
		SysUser sysuser = userutils.getUser();
		journalutils.journal1("登入",sysuser.getName()+"登入系统");
		return "redirect:/main1";
	}
	
	@RequestMapping(value="/main1")
	public String main1(){
		return "main";
	}
	/**
	 * 欢迎页面
	 * @return
	 */
	@RequestMapping(value="/home")
	public String home(ModelMap map) {
		List<Book> books = bookservice.findAll();
		map.put("books", books);
		return "home";
	 }
	
	@RequestMapping(value="/changepwd")
	public String changepwd() {
		return "changepwd";
	}
	
	@RequestMapping(value="/pwd")
	@ResponseBody
	public Boolean pwd(String password) {
		String user = userutils.getUser().getPassword(); //获取该用户的密码
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(); //密码加密器
		boolean f = encoder.matches(password,user); //比较密码是否一致
		return f;
	}
	
	@RequestMapping(value="/pwd2")
	public String changepwd1(String pwd) {
		SysUser user = userutils.getUser(); //获取到该用户
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		user.setPassword(encoder.encode(pwd)); //存入新密码
		sysuserDAO.save(user);
		SysUser sysuser = userutils.getUser();
		journalutils.journal1("修改密码",sysuser.getName()+"修改密码");
		return "redirect:logout";
	}
	
	@RequestMapping(value="/logout1")
	public String logout1() {
		SysUser sysuser = userutils.getUser();
		journalutils.journal1("登出",sysuser.getName()+"登出系统");
		return "redirect:/logout";
	}
	@RequestMapping("/logout")
	public String logout() {
		return "logout";
	}
	
	@RequestMapping(value="/findpwd")
	public String findpwd() {
		return "find/findpwd";
	}
	
	@RequestMapping(value="/yx")
	@ResponseBody
	public String yx(String username) {
		//建立邮件消息
		SimpleMailMessage mainMessage = new SimpleMailMessage();
		//发送者
		mainMessage.setFrom(mailusername);
		//接收者
		mainMessage.setTo(username);
		//发送的标题
		mainMessage.setSubject("找回密码验证码");
		String code = VerifyCodeUtils.generateVerifyCode(6);
		//发送的内容
		mainMessage.setText("请记住验证码："+code);
		System.out.println(code);
		return code;
	}
	
	@RequestMapping(value="/username")
	@ResponseBody
	public Boolean username(String username) {
		SysUser user = sysuserDAO.findByUsername(username);
		//判断数据库中是否存在该用户
		if(user == null) {
			return false;
		}else {
			return true;
		}
	}
	
	@RequestMapping(value="/zhmm1")
	@ResponseBody
	public String zhmm1(String username, String password) {
		SysUser user = sysuserDAO.findByUsername(username); //用username查找用户
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();  //密码加密器
		user.setPassword(encoder.encode(password)); //存入新密码
		sysuserDAO.save(user);
		return "密码找回成功！";
	}
	
	@RequestMapping(value="/ATmessage")
	public String ATmessage(ModelMap map) {
		Integer id = userutils.getUser().getId();
		SysUser user = sysuserservice.findById(id);
		SysRole role = roleDAO.findById(1).get(); //给权限
		map.put("role", role);
		map.put("user", user);
		return "ATmessage";
	}
}
